import { getPlayerData, isPlayerExist, storagePlayerData, timestampToSeconds } from '../l.js'

export class Fs extends plugin {
    constructor() {
        super({
            name: '[Fs]浇水',
            dsc: '浇水',
            event: 'message',
            priority: 1,
            rule: [
                { reg: /^(#|\/)?浇水\*(\d+)$/, fnc: 'watering' },
                { reg: /^(#|\/)?(一键|全部)浇水$/, fnc: 'wateringAll' }
            ]
        })
    }

    async watering(e) {
        const PLAYER_ID = e.user_id
        if (!isPlayerExist(PLAYER_ID)) {
            return e.reply('无玩家信息，请发送[领取田地]')
        }

        const [, , SERIAL_NUMBER] = e.msg.match(/^(#|\/)?浇水\*(\d+)$/)
        const PLAYER_DATA = await getPlayerData(PLAYER_ID)

        if (PLAYER_DATA['frame'].length < SERIAL_NUMBER) {
            return e.reply('你没有此格子喵~')
        }

        const frame = PLAYER_DATA['frame'][SERIAL_NUMBER - 1]
        if (!frame['start']) {
            return e.reply('该格子未种植作物喵~')
        }

        const currentTime = timestampToSeconds(Date.now())
        const lastWateringTime = frame['lastWatering'] || 0
        const timeSinceLastWatering = currentTime - lastWateringTime

        if (timeSinceLastWatering < 3600) { // 1小时CD
            return e.reply(`该格子浇水CD中，还需要${Math.ceil((3600 - timeSinceLastWatering) / 60)}分钟`)
        }

        // 根据间隔时间计算效果
        let timeReduction = 0
        if (timeSinceLastWatering >= 86400) { // 24小时以上
            timeReduction = 0.3 // 减少30%成熟时间
        } else if (timeSinceLastWatering >= 43200) { // 12小时以上
            timeReduction = 0.2 // 减少20%成熟时间
        } else {
            timeReduction = 0.1 // 减少10%成熟时间
        }

        const remainingTime = frame['time'] - currentTime
        if (remainingTime <= 0) {
            return e.reply('作物已经成熟啦，快去收获吧喵~')
        }

        frame['time'] = Math.floor(currentTime + remainingTime * (1 - timeReduction))
        frame['lastWatering'] = currentTime
        storagePlayerData(PLAYER_ID, PLAYER_DATA)

        return e.reply([
            '浇水成功!',
            `格子[${SERIAL_NUMBER}]的${frame['name']}`,
            `成熟时间缩短了${Math.floor(timeReduction * 100)}%`,
            `距离成熟还需要${Math.ceil((frame['time'] - currentTime) / 60)}分钟喵~`
        ].join('\n'))
    }

    async wateringAll(e) {
        const PLAYER_ID = e.user_id
        if (!isPlayerExist(PLAYER_ID)) {
            return e.reply('无玩家信息，请发送[领取田地]')
        }

        const PLAYER_DATA = await getPlayerData(PLAYER_ID)
        const currentTime = timestampToSeconds(Date.now())
        let successCount = 0
        let cdCount = 0
        let matureCount = 0

        for (let i = 0; i < PLAYER_DATA['frame'].length; i++) {
            const frame = PLAYER_DATA['frame'][i]
            if (!frame['start']) {
                continue
            }

            const lastWateringTime = frame['lastWatering'] || 0
            const timeSinceLastWatering = currentTime - lastWateringTime
            const remainingTime = frame['time'] - currentTime

            if (timeSinceLastWatering < 3600) {
                cdCount++
                continue
            }
            if (remainingTime <= 0) {
                matureCount++
                continue
            }

            let timeReduction = timeSinceLastWatering >= 86400 ? 0.3
                : timeSinceLastWatering >= 43200 ? 0.2
                    : 0.1

            frame['time'] = Math.floor(currentTime + remainingTime * (1 - timeReduction))
            frame['lastWatering'] = currentTime
            successCount++
        }

        if (successCount === 0) {
            if (cdCount > 0) {
                return e.reply(`所有作物都在CD中，请稍后再试`)
            }
            if (matureCount > 0) {
                return e.reply(`所有作物都已成熟，请先收获`)
            }
            return e.reply('没有可以浇水的作物喵~')
        }

        storagePlayerData(PLAYER_ID, PLAYER_DATA)
        return e.reply([
            `一键浇水成功！`,
            `成功浇水: ${successCount}块田地`,
            cdCount > 0 ? `CD中: ${cdCount}块田地` : '',
            matureCount > 0 ? `已成熟: ${matureCount}块田地` : ''
        ].filter(Boolean).join('\n'))
    }
} 